<?php
ini_set('memory_limit', '-1');
$MUSICPATH = 'D:/lighttpd/htdocs/music/';
include('db.php');
foreach($_FILES as $upload){
    //Check that we have a file
    if((!empty($upload)) && ($upload['error'] == 0)) {
        //Check if the file is mp3
        $filename = ltrim(basename($upload['name']));
        $file_basename = mysql_real_escape_string(substr($filename, 0, strripos($filename, '.'))); // strip extention and starting spaces
        $file_ext = substr($filename, -1 * strripos($filename, '.'));
        $genre = mysql_real_escape_string(ltrim($_POST['Genre']));
        $ext = mysql_real_escape_string(substr($filename, strrpos($filename, '.') + 1));
        $newname = $MUSICPATH . $filename;
        if ($ext == 'mp3' or $ext == 'wav')
        {
            $query = 'SELECT * FROM songlist WHERE title = "' . $file_basename . '"';
            $result = mysql_query($query) or die(mysql_error());
            if (mysql_affected_rows() == 0)
            {
                //Attempt to move the uploaded file to it's new place
                if ((move_uploaded_file($upload['tmp_name'],$newname)))
                {
                    echo 'File uploaded - ' . $upload['name'] . ' - ' . $filename . ' - ' . $htmlstream . ' - ' . $mp3name . ' - ' . $ext . '<br /> Your song has been put into the song list! PHP File by: Sean, WaRxHeAd <br />';
                    $query = 'INSERT INTO songlist (title, genre, submitted, extension) VALUES ("' . $file_basename . '", "' . $genre . '", 0, "' . $ext . '")';
                    mysql_query($query) or die(mysql_error());
                    $query = 'SELECT songid FROM songlist WHERE title = "' . $file_basename . '"';
                    $result = mysql_query($query) or die(mysql_error());
                    $row = mysql_fetch_row($result);
                    $songid = $row[0];
                    $query = 'SELECT songidlist FROM playlists WHERE playlistname = "' . $genre . '"';
                    $result = mysql_query($query) or die(mysql_error());
                    if (mysql_affected_rows() == 0)
                    {
                        $query = 'INSERT INTO playlists (playlistname,songidlist,submitted) VALUES ("' . $genre . '", "' . $songid . '", 0)';
                        mysql_query($query) or die(mysql_error());
                    }
                    else
                    {
                        $row = mysql_fetch_row($result);
                        $songidlist = $row[0];
                        $newsongidlist = $songidlist . ',' . $songid;
                        $query = 'UPDATE playlists SET songidlist = "' . $newsongidlist . '" WHERE playlistname = "' . $genre . '"';
                        mysql_query($query) or die(mysql_error());
                    }
                }
                else
                {
                   echo 'Error: A problem occurred during file upload!';
                }
            }
            else
            {
                echo 'Error: File ' . $upload['name'] . ' already exists';
            }
        }
        else
        {
        echo 'Error: Only .mp3 or .wav music files are accepted for upload';
        }
    }
}
?>